home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / answrbok / 6_10.lha / 6_10 / 6_10in.c < prev    next >
Text File  |  1993-08-08  |  3KB  |  140 lines

  1. * Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */
  2. * The C++ Answer Book */
  3. * Tony Hansen */
  4. * All rights reserved. */
  5. / Input a LINT.
  6. / Convert a string of signed digits into a LINT.
  7. / Hex and octal prefixes are allowed.
  8. include <ctype.h>
  9. include <error.h>
  10. include <lint.h>
  11.  
  12. / convert hexadecimal 0-9, a-f, A-F
  13. / to its numeric value
  14. nline int hexvalue(char c)
  15.  
  16.    if (isdigit(c))
  17. return c - '0';
  18.    else if (c >= 'a' && c <= 'f')
  19. return c - 'a' + 10;
  20.    else
  21. return c - 'A' + 10;
  22.  
  23.  
  24. / return true if c is an octal digit
  25. nline int isodigit(int c)
  26.  
  27.    return isdigit(c) && c < '8';
  28.  
  29.  
  30. / perform: i = i * base - newdigit
  31. tatic void addonedigit(LINT_type *i_s, int multiplier, int addend)
  32.  
  33.    // i *= base
  34.    // almost identical to that used
  35.    // for operator*
  36.    LINT_Ltype k = 0;
  37.    for (int l = 3; ; )
  38. {
  39. /                                            cerr << "l=" << l << "\n";    /* DELETE */
  40. /                                            cerr << "k=" << form("0x%8.8x", k) << "\n";    /* DELETE */
  41. /                                            cerr << "s[l]=" << form("0x%4.4x", s[l]) << "\n";    /* DELETE */
  42. LINT_Ltype t = i_s[l];
  43. t *= multiplier;
  44. t += k;
  45. /                                                                cerr << "t=" << form("0x%8.8x", t) << "\n";    /* DELETE */
  46. i_s[l] = t;    // % LINT_base
  47. if (--l < 0)
  48.     break;
  49. k = t / LINT_base;
  50. }
  51.  
  52.    // i -= addend
  53.    // almost identical to that used
  54.    // for operator-
  55.    k = addend;
  56.    for (l = 3; ; )
  57. {
  58. /                                        cerr << "l=" << l << "\n";    /* DELETE */
  59. /                                        cerr << "k=" << form("0x%8.8x", k) << "\n";    /* DELETE */
  60. /                                        cerr << "s[l]=" << form("0x%4.4x", s[l]) << "\n";    /* DELETE */
  61. LINT_Ltype t = i_s[l] - k;
  62. /                                                                cerr << "t=" << form("0x%8.8x", t) << "\n";    /* DELETE */
  63. i_s[l] = t;
  64. if (--l < 0)
  65.     break;
  66. k = (t / LINT_base) ? 1 : 0;
  67. }
  68.  
  69.  
  70. stream& operator>> (istream& in, LINT& i)
  71.  
  72.    char c, sign = 0;
  73.    i = 0;
  74. /                                                cerr << "starting input\n";    /* DELETE */
  75.    in >> WS;        // skip white space
  76.    if (!in.get(c))
  77. return in;
  78.  
  79. /                                                cerr << "got character '" << chr(c) << "'\n";    /* DELETE */
  80.    switch (c)        // remember the sign
  81. {
  82. case '+': case '-':
  83.     sign = c;
  84. /                                                cerr << "got sign\n";    /* DELETE */
  85.     if (!in.get(c))
  86.     return in;
  87. /                                                cerr << "got character '" << chr(c) << "'\n";    /* DELETE */
  88.     break;
  89. }
  90.  
  91.    switch (c)
  92. {
  93. case '0':        // hex or octal
  94. /                                                cerr << "hex or octal\n";    /* DELETE */
  95.     if (!in.get(c))
  96.     break;
  97. /                                                cerr << "got character '" << chr(c) << "'\n";    /* DELETE */
  98.  
  99.     switch (c)
  100.     {
  101.     case 'x':        // hex number
  102.     case 'X':
  103. /                                                cerr << "hex\n";    /* DELETE */
  104.         for (in.get(c);
  105.          in && isxdigit(c);
  106.          in.get(c))
  107.         addonedigit(i.s, 16, hexvalue(c));
  108.         break;
  109.  
  110.     default:        // octal number
  111. /                                                cerr << "octal\n";    /* DELETE */
  112.         for ( ; in && isodigit(c);
  113.          in.get(c))
  114.         addonedigit(i.s, 8, (c - '0'));
  115.         break;
  116.     }
  117.     break;
  118.  
  119. default:        // decimal number
  120. /                                                cerr << "decimal\n";    /* DELETE */
  121.     for ( ; in && isdigit(c); in.get(c))
  122.     addonedigit(i.s, 10, (c - '0'));
  123.     break;
  124. }
  125.  
  126.    if (in)
  127. {    /* DELETE */
  128. /                                                cerr << "putting back '" << chr(c) << "'\n";    /* DELETE */
  129. in.putback(c);
  130. }    /* DELETE */
  131.  
  132.    if (sign != '-')
  133. {    /* DELETE */
  134. /                                                cerr << "positive number\n";    /* DELETE */
  135. i = -i;
  136. }    /* DELETE */
  137.  
  138.    return in;
  139.  
  140.